﻿/* Color 扩展类
 */

/* 未完事项
 * 1、单元测试。
 */
namespace LSLib.Drawing
{
    using System;
    using System.Drawing;

    public class ColorEx
    {
        /// <summary>获取对应偏移量的RGB颜色
        /// </summary>
        public static Color GetOffsetRGB(Color oldColor, int offset)
        {
            if (System.Math.Abs(offset) > byte.MaxValue)
                throw new ArgumentOutOfRangeException("offset", offset, string.Format("偏移量不应超过：{0}", byte.MaxValue));

            byte[] oldRGB = new byte[3] { oldColor.R, oldColor.G, oldColor.B };
            byte[] newRGB = new byte[3];
            for (int i = 0; i < 3; i++)
            {
                int stmp = oldRGB[i] + offset;
                if (stmp < byte.MinValue) stmp = byte.MinValue;
                else if (stmp > byte.MaxValue) stmp = byte.MaxValue;
                newRGB[i] = (byte)stmp;
            }
            return Color.FromArgb(newRGB[0], newRGB[1], newRGB[2]);
        }
    }
}
